package com.auth.token.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.auth.dao.mapper.TokenMapper;
import com.auth.token.app.AppTokenClient;
import com.auth.token.app.AppTokenProperties;
import com.auth.token.app.UserInfo;
import com.auth.token.base.AccessTokenEntity;
import com.auth.token.base.RefreshTokenEntity;
import com.auth.token.base.TokenGenerator;
import com.auth.token.base.TokenUuidMd5Generator;
import com.auth.token.device.DeviceInfo;
import com.auth.token.device.DeviceTokenClient;
import com.auth.token.device.DeviceTokenProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @author: 王霄
 * @date: 2022/1/20 18:36
 */
@Configuration
@EnableConfigurationProperties(value = {AppTokenProperties.class, DeviceTokenProperties.class})
public class TokenConfig {

    @Resource
    private TokenMapper tokenMapper;

    @Resource
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate<String, AccessTokenEntity<?>> atRedisTemplate() {
        RedisTemplate<String, AccessTokenEntity<?>> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(AccessTokenEntity.class));
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public RedisTemplate<String, RefreshTokenEntity<?>> rtRedisTemplate() {
        RedisTemplate<String, RefreshTokenEntity<?>> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(RefreshTokenEntity.class));
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public AppTokenClient appTokenRedisClient(AppTokenProperties properties,
                                              RedisTemplate<String, AccessTokenEntity<?>> atRedisTemplate,
                                              RedisTemplate<String, RefreshTokenEntity<?>> rtRedisTemplate) {
        TokenGenerator tokenGenerator = new TokenUuidMd5Generator();
        return new AppTokenClient(properties, tokenGenerator, atRedisTemplate, rtRedisTemplate, UserInfo.class);
    }

    @Bean
    public DeviceTokenClient deviceTokenClient(DeviceTokenProperties properties) {
        TokenGenerator tokenGenerator = new TokenUuidMd5Generator();
        return new DeviceTokenClient(properties, tokenGenerator, tokenMapper, DeviceInfo.class);
    }
}
